#include "widget.h"
#include "ui_widget.h"
#include <QShortcut>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 设置快捷键来调整滑动条
    ui->horizontalSlider->setMinimum(0);
    ui->horizontalSlider->setMaximum(1000);

    // 给滑动条设置快捷键
    // 需要两个快捷键，通过-进行减小，通过+进行增加

    // 创建两个快捷键对象
    QShortcut* shortcut1 = new QShortcut(this);
    QShortcut* shortcut2 = new QShortcut(this);
    shortcut1->setKey(QKeySequence("-"));
    shortcut2->setKey(QKeySequence("+"));

    // 使用信号槽，感知到快捷键被按下
    connect(shortcut1, &QShortcut::activated, this, &Widget::subValue);
    connect(shortcut2, &QShortcut::activated, this, &Widget::AddValue);


}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_horizontalSlider_valueChanged(int value)
{
    ui->label->setText("当前的值为: " + QString::number(value));
}

void Widget::subValue()
{
    int val = ui->horizontalSlider->value();
    if (val <= ui->horizontalSlider->minimum()) {
        return;
    }
    ui->horizontalSlider->setValue(val - 1);
}

void Widget::AddValue()
{
    int val = ui->horizontalSlider->value();
    if (val >= ui->horizontalSlider->maximum()) {
        return;
    }
    ui->horizontalSlider->setValue(val + 1);
}
